package com.fsl.sicotec.UI;

import com.fsl.sicotec.comun.Entidades.Tecnico;
import com.fsl.sicotec.Logica.LogTecnico;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author User
 */
public class jfListadoTecnicos extends javax.swing.JDialog {
//Declarando las variables

    /** Creates new form jfListadoTecnicos */
    public jfListadoTecnicos(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        //Instanciando las variables
        listarTecnicos();

    }

    public void listarTecnicos() {
        DefaultTableModel mdlTecnico = new DefaultTableModel();
        LogTecnico objLogTecnico = new LogTecnico();
        ArrayList<Tecnico> arrTecnicos = new ArrayList();
        try {
            mdlTecnico = (DefaultTableModel) jtLista.getModel();
            jtLista.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
            arrTecnicos = objLogTecnico.getTecnicos();
            mdlTecnico.setRowCount(arrTecnicos.size());
            int i = 0;
            for (Tecnico objTecnico : arrTecnicos) {
                mdlTecnico.setValueAt(objTecnico.getCod_tecnico(), i, 0);
                mdlTecnico.setValueAt(objTecnico.getDni(), i, 1);
                mdlTecnico.setValueAt(objTecnico.getApe_paterno(), i, 2);
                mdlTecnico.setValueAt(objTecnico.getApe_materno(), i, 3);
                mdlTecnico.setValueAt(objTecnico.getNombres(), i, 4);
                mdlTecnico.setValueAt(objTecnico.getDireccion(), i, 5);
                mdlTecnico.setValueAt(objTecnico.getTelefono(), i, 6);
                i++;
            }
            this.jtLista.setModel(mdlTecnico);
        } catch (Exception e) {
            javax.swing.JOptionPane.showMessageDialog(null, e.getMessage());
        } finally {
            mdlTecnico = null;
            objLogTecnico = null;
            arrTecnicos = null;
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        btnNuevo = new javax.swing.JButton();
        btnModificar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        btnImprimir = new javax.swing.JButton();
        btnCerrar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtLista = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Listado de Técnicos");

        jToolBar1.setRollover(true);

        btnNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/fsl/sicotec/UI/Recursos/agregar.png"))); // NOI18N
        btnNuevo.setText("Nuevo");
        btnNuevo.setFocusable(false);
        btnNuevo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnNuevo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoActionPerformed(evt);
            }
        });
        jToolBar1.add(btnNuevo);

        btnModificar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/fsl/sicotec/UI/Recursos/editar.png"))); // NOI18N
        btnModificar.setText("Modificar");
        btnModificar.setFocusable(false);
        btnModificar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnModificar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarActionPerformed(evt);
            }
        });
        jToolBar1.add(btnModificar);

        btnEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/fsl/sicotec/UI/Recursos/eliminar.png"))); // NOI18N
        btnEliminar.setText("Eliminar");
        btnEliminar.setFocusable(false);
        btnEliminar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnEliminar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });
        jToolBar1.add(btnEliminar);

        btnImprimir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/fsl/sicotec/UI/Recursos/PrintHS.png"))); // NOI18N
        btnImprimir.setText("Imprimir");
        btnImprimir.setFocusable(false);
        btnImprimir.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnImprimir.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(btnImprimir);

        btnCerrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/fsl/sicotec/UI/Recursos/exit.png"))); // NOI18N
        btnCerrar.setText("Cerrar");
        btnCerrar.setFocusable(false);
        btnCerrar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnCerrar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCerrarActionPerformed(evt);
            }
        });
        jToolBar1.add(btnCerrar);

        jtLista.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "Código", "DNI", "Ap. Paterno", "Ap. Materno", "Nombres", "Dirección", "Teléfono"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jtLista);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 770, Short.MAX_VALUE)
                .addGap(20, 20, 20))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 490, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(21, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed
        jdTecnico vistaTecnico = new jdTecnico(this, true);
        vistaTecnico.setLocationRelativeTo(this);
        // vista.setSize(480, 300);
        vistaTecnico.setVisible(true);
        listarTecnicos();
    }//GEN-LAST:event_btnNuevoActionPerformed

    private void btnModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarActionPerformed
        LogTecnico objLogTecnico = new LogTecnico();
        int fila = jtLista.getSelectedRow();
        String strDNI;
        String Nombre;
        if (fila != -1) {
            strDNI = jtLista.getValueAt(fila, 1).toString();
            jdTecnico vistaTecnico = new jdTecnico(this, true,strDNI);
            vistaTecnico.setLocationRelativeTo(this);
            // vista.setSize(480, 300);
            vistaTecnico.setVisible(true);
                listarTecnicos();
        } else {
            javax.swing.JOptionPane.showMessageDialog(null, "Debe seleccionar un cliente");
        }

}//GEN-LAST:event_btnModificarActionPerformed

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
        LogTecnico objLogTecnico = new LogTecnico();
        int fila = jtLista.getSelectedRow();
        String strDNI;
        String Nombre;
        if (fila != -1) {
            strDNI = jtLista.getValueAt(fila, 1).toString();
            Nombre = jtLista.getValueAt(fila, 1).toString() + " " + jtLista.getValueAt(fila, 2).toString() + " , " + jtLista.getValueAt(fila, 3).toString();
            int result = javax.swing.JOptionPane.showConfirmDialog(null, "¿Esta seguro de eliminar el cliente : \n" + Nombre + "?");
            if (result == 0) {
                objLogTecnico.eliminarTecnico(strDNI);
                //javax.swing.JOptionPane.showMessageDialog(null, "Tecnico eliminado correctamente");
                listarTecnicos();
            }
        } else {
            javax.swing.JOptionPane.showMessageDialog(null, "Debe seleccionar un cliente");
        }
}//GEN-LAST:event_btnEliminarActionPerformed

    private void btnCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCerrarActionPerformed
        setVisible(false);
}//GEN-LAST:event_btnCerrarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                jfListadoTecnicos dialog = new jfListadoTecnicos(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCerrar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnImprimir;
    private javax.swing.JButton btnModificar;
    private javax.swing.JButton btnNuevo;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTable jtLista;
    // End of variables declaration//GEN-END:variables
}
